package com.xckj.teacher.util

import android.os.SystemClock
import android.text.Editable
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.xckj.teacher.R

object CustomBindAdapter {

    @BindingAdapter(value = ["checkChange"])
    @JvmStatic
    fun checkChange(checkbox: CheckBox, listener: CompoundButton.OnCheckedChangeListener) {
        checkbox.setOnCheckedChangeListener(listener)
    }

    @BindingAdapter(value = ["showPwd"])
    @JvmStatic
    fun showPwd(view: EditText, boolean: Boolean) {
        if (boolean) {
            view.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        } else {
            view.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
//        view.setSelection(view.textString().length)
    }

    @BindingAdapter(value = ["imageUrl"])
    @JvmStatic
    fun imageUrl(view: ImageView, url: String?) {
        if (url.isNullOrEmpty()) return
        Glide.with(view.context.applicationContext)
            .load(url)
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .placeholder(R.mipmap.default_avatar)
            .error(R.mipmap.default_avatar)
            .into(view)
    }

    @BindingAdapter(value = ["drawableUrl"])
    @JvmStatic
    fun drawableUrl(view: ImageView, drawable: Int) {
        Glide.with(view.context.applicationContext)
            .load(drawable)
            .into(view)
    }

    @BindingAdapter(value = ["circleImageUrl"])
    @JvmStatic
    fun circleImageUrl(view: ImageView, url: String?) {
        if (TextUtils.isEmpty(url)) return
        if (url?.startsWith("data:image/svg+xml;base64,")==true) {
            url.removePrefix("data:image/svg+xml;base64,")
        }
        Glide.with(view.context.applicationContext)
            .load(url)
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .into(view)
    }

    @BindingAdapter(value = ["cornerImageUrl"])
    @JvmStatic
    fun cornerImageUrl(view: ImageView, url: String) {
        LogUtils.e("url:${url}")

        if (url.isNullOrEmpty()) return
        try {
            if (url.startsWith("data:image/svg+xml;base64,")) {
                url.removePrefix("data:image/svg+xml;base64,")
            }
            Glide.with(view.context.applicationContext)
                .load(url)
                .centerCrop()
                .apply(
                    RequestOptions.bitmapTransform(
                        CornerTransform(
                            view.context.applicationContext,
                            SizeUtils.dp2px(15f).toFloat()
                        )
                    )
                )
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .into(view)
        } catch (e: Exception) {
            LogUtils.e(e.message)
        }

    }

    @BindingAdapter(value = ["afterTextChanged"])
    @JvmStatic
    fun EditText.afterTextChanged(action: (String) -> Unit) {
        addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                action(s.toString())
            }
        })
    }

    @BindingAdapter(value = ["noRepeatClick"])
    @JvmStatic
    fun setOnClick(view: View, clickListener: () -> Unit) {
        val mHits = LongArray(2)
        view.setOnClickListener {
            System.arraycopy(mHits, 1, mHits, 0, mHits.size - 1)
            mHits[mHits.size - 1] = SystemClock.uptimeMillis()
            if (mHits[0] < SystemClock.uptimeMillis() - 500) {
                clickListener.invoke()
            }
        }
    }


}